<?php
require_once("_connect.php");
if (true)
{
	$order_no = getMax($conn);
	$ordcamph = "201420";		
	$datetime = explode(" ", "31/12/2014 02:03:01");
	$orddate = wrapperDate($datetime[0]);
	$ordtime = str_replace(":","",$datetime[1]);
	$curcamp = getCurCamp($conn);
	$dist = "4000";
	$mslno = "00067";
	$chkdgt = "5";
	$memberno = "4000-00067-5";
	$membername = "test";
	$totalamount = 0;	

	$rows = genDetail(0);
	$i=0;
	if (HeaderExists($conn,$order_no,$ordcamph,$dist,$mslno,$chkdgt))
	{
		$respond_message = "DUPLICATE";
		$error_message = "DUPLICATE! ".$order_no."/".$ordcamph."(".$dist."-".$mslno."-".$chkdgt.")";
	}
	else
	{
	    mysql_query("BEGIN");        
		foreach($rows AS $rowNum => $row)
		{
	        $LISTNO = ++$i;
	        $BILLCODE = $row['BILLCODE'];
	        $BILLDESC = $row['BILLDESC'];
	        $QTY = $row['QTY'];        
	        $PRICE = $row['PRICE'];
	        $AMOUNT = str_replace(",","", $row['AMOUNT']);
	        $ORDCAMPD = $row['ORDCAMPD'];
	        $REMARK = 'N';
	        $BRAND = 'N';
	        $DISCOUNT = 'NULL';
	        $SPCFLG = 'N';
	        $DISCFLG = 'N';
	        $INCTFLG = 'N';
	        $FREEFLG = 'N';
	        $BILLFLG = 'N';
	        $DWNFLG = 'N';
	        $EXPFLG = 'N';
	        $FLAG1 = 'N';
	        $DELFLAG = 'N';
	        
	        $sql = "INSERT INTO order_detail_tmp (
	        	ORDER_NO ,
				ORDCAMPH ,
				ORDDATE ,
				ORDTIME ,
				CURCAMP ,
				DIST ,
				MSLNO ,
				CHKDGT ,
				LISTNO ,
				BILLCODE ,
				BILLDESC ,
				QTY ,
				PRICE ,
				AMOUNT ,
				REMARK ,
				BRAND ,
				DISCOUNT ,
				SPCFLG ,
				DISCFLG ,
				INCTFLG ,
				FREEFLG ,
				BILLFLAG ,
				DWNFLAG ,
				EXPFLAG ,
				FLAG1 ,
				DELFLAG ,
				ORDCAMPD
	        ) VALUES (
	        	$order_no,  
	        	$ordcamph,  
	        	$orddate,  
	        	'".$ordtime."',  
	        	$curcamp,  
	        	'".$dist."',  
	        	$mslno,  
	        	$chkdgt,  
	        	$LISTNO,  
	        	'".$BILLCODE."',  
	        	'".$BILLDESC."',  
	        	$QTY,  
	        	$PRICE,  
	        	$AMOUNT,  
	        	'".$REMARK."', 
	        	'".$BRAND."' ,  
	        	$DISCOUNT,  
	        	'".$SPCFLG."',  
	        	'".$DISCFLG."',  
	        	'".$INCTFLG."',  
	        	'".$FREEFLG."',  
	        	'".$BILLFLG."',  
	        	'".$DWNFLG."',  
	        	'".$EXPFLG."',  
	        	'".$FLAG1."',  
	        	'".$DELFLAG."',  
	        	$ORDCAMPD
	        )";
	    	$result = mysql_query($sql, $conn);
	    	if (!$result)
	    	{
	        	echo $sql;	    		
	    		$totalamount = $totalamount + $AMOUNT;
				mysql_query("ROLLBACK");	    		
	    		$respond_message = "ERROR";
				$error_message = "ROLLBACK! D".$order_no."/".$ordcamph."(".$dist."-".$mslno."-".$chkdgt.")";
				echo mysql_errno($conn) . ": " . mysql_error($conn). "\n";
				break;
			}
		}
		
		if ($result)
		{
			$sql = "INSERT INTO  order_header_tmp (
				ORDER_NO ,
				ORDCAMP ,
				ORDDATE ,
				ORDTIME ,
				CURCAMP ,
				DIST ,
				MSLNO ,
				CHKDGT ,
				NAME ,
				ITEMS ,
				TOTAL_AMOUNT ,
				AMOUNT_1 ,
				AMOUNT_2 ,
				AMOUNT_3 ,
				DISCOUNT_1 ,
				DISCOUNT_2 ,
				DISCOUNT_3 ,
				BILLDATE ,
				SHIPDATE ,
				DLVDATE ,
				DWNDATE ,
				DWNFLAG ,
				EXPFLAG ,
				MAIL_CONFIRM ,
				UPDDATE ,
				UPDTIME ,
				DP_DOWNLOAD ,
				WEBSITE_ID ,
				DELFLAG ,
				BROWSER ,
				ACCOUNT_ID
			) VALUES (
	        	$order_no,  
	        	$ordcamph,  
	        	$orddate,  
	        	'".$ordtime."',  
	        	$curcamp,  
	        	'".$dist."',  
	        	$mslno,  
	        	$chkdgt,
	        	'".$membername."',
	        	$i,
	        	$totalamount,
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				'N',
				'N',
				'N',
				'".date("Ymd")."',
				'".date("His")."',
				'',
				'',
				'N',
				'',
				'".$_SESSION["RACCOUNT_ID"]."'		
			)";
			
			$result = mysql_query($sql, $conn);
	    	if (!$result)
	    	{
	    		echo $sql;
				mysql_query("ROLLBACK");	    		
	    		$respond_message = "ERROR";
				$error_message = "ROLLBACK! H".$order_no."/".$ordcamph."(".$dist."-".$mslno."-".$chkdgt.")";
				echo mysql_errno($conn) . ": " . mysql_error($conn). "\n";
			}
			else
			{
				mysql_query("COMMIT");
				$respond_message = "SUCCESSFULL";
			}
		}		
	}
}

function HeaderExists($conn,$order_no,$ordcamph,$dist,$mslno,$chkdgt)
{
	$sql = "SELECT count(*) as numrow 
	FROM order_header_tmp oh 
		WHERE 
		oh.DIST ='".$dist."'  
	AND oh.MSLNO = ".$mslno." 
	AND oh.CHKDGT = ".$chkdgt." 
	AND oh.ORDCAMP = ".$ordcamph." 
	AND oh.ORDER_NO = ".$order_no;
	//echo $sql;
	$raw = mysql_query($sql, $conn);
	$row = mysql_fetch_array($raw, MYSQL_ASSOC);
	if ($row["numrow"] > 0)
	{
		return false;
	}
	else
	{
		return false;
	}
}
?>
<!DOCTYPE html>
<html lang="th" xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta charset="utf-8" />
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta http-equiv="CACHE-CONTROL" content="NO-CACHE">
	<meta http-equiv="EXPIRES" content="Mon, 22 Jul 2002 11:12:01 GMT">
	<meta http-equiv="pragma" content="no-cache" />    
	<title>TOS-Result</title>	
</head>
<body>
<?php
if ($respond_message == 'ERROR')
{
	echo "<h2><p>ผิดพลาด! ไม่สามารถบันทึกได้</p></h2>";
	echo "<a href='quickorder.php'>ย้อนกลับ</a>";
} 
else if ($respond_message == 'DUPLICATE') 
{
	echo "<h2><p>ผิดพลาด! ใบสั่งซื้อถูกบันทึกก่อนหน้านี้แล้ว</p></h2>";
	echo "<a href='quickorder.php'>ย้อนกลับ</a>";	
}
else if ($respond_message == 'SUCCESSFULL')
{
	echo "<h2><p>บันทึกสมบูรณ์ ต้องการทำรายการใหม่หรือไม่?</p></h2>";
	echo "<a href='quickorder.php'>ทำรายการใหม่</a> | ";
	echo "<a href='editorder.php?dist=$dist&mslno=$mslno&chkdgt=$chkdgt&ordcamp=$ordcamph&order_no=$order_no'>
		แก้ไขรายการเดิม</a>";			
}
else
{
	echo "<script>window.location.href='quickorder.php';</script>";
}
?>
</body>
</html>
<?php
	function genDetail()
	{
		for ($i=1; $i<100; $i++)
		{
	        $rows[$i] = array(
	            'BILLCODE'	=>str_pad($i,5,0,STR_PAD_LEFT), 
	            'QTY'		=>100, 
	            'BILLDESC'	=>"test", 
	            'PRICE'		=>100,
	            'AMOUNT'	=>10000,
	            'ORDCAMPD'	=>201420
	        );		        	
		}
		return $rows;		
	}
	
function displayCamp($camp)
{
	$no = substr($camp, 4, 2);
	$yr = substr($camp, 0, 4);
	return $no."/".$yr;
}

function mergeMsl($dist, $mslno, $chkdgt)
{
	return str_pad($dist,4,0,STR_PAD_LEFT)."-".str_pad($mslno,5,0,STR_PAD_LEFT)."-".$chkdgt;
}

function getOrderNo($conn, $dist, $mslno, $chkdgt, $ordcamp)
{
	$sql = "SELECT max(oh.ORDER_NO) AS order_no 
		FROM order_header oh 
			WHERE 
			oh.DIST ='".$dist."'  
		AND oh.MSLNO = ".$mslno." 
		AND oh.CHKDGT = ".$chkdgt." 
		AND oh.ORDCAMP = ".$ordcamp."
		AND oh.DWNFLAG = 'N'";
		//echo $sql;
	$raw = mysql_query($sql, $conn);
	$row = mysql_fetch_array($raw, MYSQL_ASSOC);
	if ($row["order_no"] > 0)
	{
		return $row["order_no"] + 1;
	}
	else
	{
		return 1001;
	}
}

function getMaxCamp($conn)
{
	$sql = "SELECT * FROM tbl008 WHERE STATUS = 'Current'";
		//echo $sql;
	$raw = mysql_query($sql, $conn);
	$row = mysql_fetch_array($raw, MYSQL_ASSOC);
	if ($row["CAMP"] > 0)
	{
		return $row["CAMP"] + 2;
	}	
}

function getMinCamp($conn)
{
	$sql = "SELECT * FROM tbl008 WHERE STATUS = 'Current'";
		//echo $sql;
	$raw = mysql_query($sql, $conn);
	$row = mysql_fetch_array($raw, MYSQL_ASSOC);
	if ($row["CAMP"] > 0)
	{
		return $row["CAMP"] - 1;
	}	
}

function getCurCamp($conn)
{
	$sql = "SELECT * FROM tbl008 WHERE STATUS = 'Current'";
		//echo $sql;
	$raw = mysql_query($sql, $conn);
	$row = mysql_fetch_array($raw, MYSQL_ASSOC);
	if ($row["CAMP"] > 0)
	{
		return $row["CAMP"];
	}	
}

function wrapperDate($str)
{
	$date = explode("/", $str);
	return $date[2].$date[1].$date[0];
}

function mergeDate($str)
{
	$date = substr($str,6,2)."/".substr($str,4,2)."/".substr($str,0,4);
	$time = substr($str,8,2).":".substr($str,10,2).":".substr($str,12,2);
	return $date." ".$time;
}	

function getMax($conn)
{
	$sql = "SELECT max(order_no) as ordno FROM order_header_tmp";
		//echo $sql;
	$raw = mysql_query($sql, $conn);
	$row = mysql_fetch_array($raw, MYSQL_ASSOC);
	if ($row["ordno"] > 0)
	{
		return $row["ordno"]+1;
	}
	else
	{
		return 1001;
	}
}
include("_close.php");
?>